//
//  main.swift
//  Swift练习
//
//  Created by apple on 16/5/25.
//  Copyright © 2016年 apple. All rights reserved.
//

import Foundation

print("Hello, World!")


//2016.05.25 笔记
#if false

var number = 1;
var name = "张三";

let 你好 = "你好张三";
//let &&& = 3.1415;


var name1 = "tom";
name1 = "kavon";

//字符串插值 \()
print("\(name1)");

print("", separator: "", terminator: "");


let minvalue = uint16.min;
let maxvalue = uint16.max;

//没有代码提示
print("minvalue:\(minvalue) , maxvalue:\(maxvalue)");


var numbervalue = Int.max;

print("int value : \(numbervalue)")

//类型检查
var number2:Float = 3.1415;
//默认Double类型
var number3 = 3.141592654;


let decimalDouble = 12.1875;
//e 的含义,10进制 e2 = 10^2;
//        16进制 e2 = 2 ^2 ;
let exponentDouble = 1.21875e1;
let hexadecimalDouble = 0xC.3p0;

print("decimalDouble : \(decimalDouble)")
print("exponentDouble : \(exponentDouble)")
print("hexadecimalDouble : \(hexadecimalDouble)")

//数值型字面量 可添加下划线 , 不影响本值
let paddedDouble  = 000123.456
let oneMillion = 1_0_0_0_0_0_0;
let justoveronemillion = 1_000_000.000_000_1;

print("paddedDouble : \(paddedDouble)")
print("oneMillion : \(oneMillion)")
print("justoveronemillion : \(justoveronemillion)")


let twothousand:uint16 = 2_000
let one:uint8 = 1
let twothowsandandone = twothousand + uint16(one)

print("twothowsandandone : \(twothowsandandone)")

let three = 3
let ponitoneFouroneFiveNine = 0.94159
let pi = Double(three) + ponitoneFouroneFiveNine;
//浮点型 转 整形 , 小数点后所有数 全部舍掉.
let pi2 = three + Int(ponitoneFouroneFiveNine);

let pi3 = 3 + 0.1415926;


print("pi : \(pi)")
print("pi2 : \(pi2)")
print("pi3 : \(pi3)")

//类型别名 , 只能给类名 起别名
typealias AudioSample = uint16;

//相当于 Uint16.max
var maxAmplitudeFound = AudioSample.max;

let orangesAreOrange:Bool = true;
let turnipsareDelicious = false;


//if判断语句  ,  也可以用if()
if turnipsareDelicious{

    print("真")
    
}else{

    print("假")

}

    
let truename = Int(true)
    
    print("truename : \(truename)")
    
#endif

//元组

let http404error = (404,"Not Found");

let (statusCode,statusMessage) = http404error;

print("statusCode : \(statusCode)")
print("statusMessage : \(statusMessage)")

//可以将元组中 , 省略的部分用 _ 表示
let (justStatusCode, _) = http404error
print("justStatusCode : \(justStatusCode)");

//通过下标访问 (数组下标 , 以http404error.0 , http404error.1 表示)
print("statusCode : \(http404error.0)");
print("statusMessage : \(http404error.1)");


//对元组 元素进行命名 , 并通过 名字获取元素值
let http200Status = (statusCode1 : 200 , description:"OK")

print("statusCode1 : \(http200Status.statusCode1)")
print("description : \(http200Status.description)")



//optionals
// 用? 来表示一个值是否缺失 , 如果缺失 则返回nil

let possibleNumber:Int? = 123;

print("是否缺失\(possibleNumber)");


//在变量后面加 ! 号表示 , 这个值确定有值, 强制解析出来.
if possibleNumber != nil{

    print("没有缺失的值: \(possibleNumber!)");

}



//if 语句 , 可以判断时进行赋值

if let firstNumber = Int("4") , secondNumber = Int("42") where firstNumber < secondNumber{
    
    print("\(firstNumber) < \(secondNumber)");

}



//断言(assert)
//如果判断为假 , 则会终止程序(程序报错EXC_BAD_INSTRUCTION)
/**适用场景
 1.整数类型的下标索引被传入一个自定义下标实现，但是下标索引值可能太小或者太大。
 2.需要给函数传入一个值，但是非法的值可能导致函数不能正常执行。
 3.一个可选值现在是nil，但是后面的代码运行需要一个非nil值。
 */

let age  = -3;

assert(age >= 0,"年龄不能为负数");




